package com.cgy.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cgy.entity.Student;
import com.cgy.entity.Xibie;
import com.cgy.service.StudentService;
import com.cgy.service.XibieService;

import util.LayuiJsonUtils;

@RestController
@RequestMapping("student") 
@CrossOrigin 
public class StudentController {
	//学生信息接口
	@Autowired
	private StudentService StuS;
	/**
	 * 多条件分页查询
	 * @param lj 工具类 转换为前台layui需要的格式
	 * @param s
	 * @return
	 */
	@GetMapping
	public LayuiJsonUtils<Student> findBysnamePage(LayuiJsonUtils<Student> lj,Student s,Xibie xb){
		s.setXibie(xb);		
		//jpa分页    lj获取页数和每页显示的条数
		Pageable pageable = PageRequest.of(lj.getPage(), lj.getLimit());
		Page<Student> stu = StuS.findBysnamePage(s, pageable);//调用jpa查询的方法
		lj.setData(stu.getContent());//将值设入到封装类里
		lj.setCount(stu.getTotalElements());
		return lj;
	}
	
	/**
	 * 添加 和修改
	 * @param s 学生实体
	 * @param xid  学生的系别id 	
	 * @return
	 */
	@PostMapping
	public Student addAndUpdateStudent(Student s,Integer xid) {
		Xibie x = new Xibie();
		x.setXid(xid);
		s.setXibie(x);
		return StuS.addAndUpdate(s);
		
	}
	 
	/**
	 * 修改学生信息
	 * @param s
	 * @param xid
	 * @return
	 */
	@PutMapping
	public Student addAndUpdateStudent1(Student s,Integer xid) {
		Xibie x = new Xibie();
		x.setXid(xid);
		
		s.setXibie(x);
		return StuS.addAndUpdate(s);
		
	}
	/**
	 * 删除接口
	 * @param sid 根据学生id
	 * @return
	 */
	@DeleteMapping("{sid}")	
	public Integer deleteStudent(@PathVariable("sid") Integer sid) {
		return  StuS.deleteStudentByID(sid);
		
	}
	

}
